<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.inc.php';
$link=connect();
if (isset($_SERVER["HTTP_REFERER"])) {
    $url = $_SERVER["HTTP_REFERER"];
} else {
    $url = "index.php";
}

if(!$member_id=is_login($link)){
    skip($url, 'error', '请登录之后再做回复!');
}
if(!isset($_GET['id']) || !is_numeric($_GET['id']) || !isset($_GET['reply_id']) || !is_numeric($_GET['reply_id'])) {
    skip($url, 'error', '您要回复的帖子id参数不合法!');
}
$query = "SELECT content from sfk_reply where id = {$_GET['reply_id']} and content_id = {$_GET['id']} limit 1";
$find = execute($link,$query);
if (!$find->num_rows) {
    skip($_SERVER["HTTP_REFERER"],'error','未找到该贴的回复信息');
}
$reply_content = $find->fetch_array();
$reply_content['content'] = nl2br(htmlspecialchars($reply_content['content']));
$query = "SELECT c.title,c.content,s.name from sfk_content c 
    LEFT JOIN sfk_member s on  c.member_id = s.id 
where c.id = {$_GET['id']} LIMIT 1";
$data_content = execute($link, $query);
if ($data_content->num_rows != 1) {
    skip($_SERVER["HTTP_REFERER"], 'error', '您要回复的帖子不存在!');
}
$data_content = $data_content->fetch_assoc();
$data_content['title'] = htmlspecialchars($data_content['title']);
$data_content['content'] = nl2br(htmlspecialchars($data_content['content']));
// 回复信息处理
if(isset($_POST['submit'])) {
    if (mb_strlen($_POST['content']) < 3) {
        skip($url, 'error', '回复信息字符不能少于3个字');
    }
    // 存储回复信息
    $_POST=escape($link,$_POST);
    $query="insert into sfk_reply(content_id, content, time, member_id, quote_reply) values({$_GET['id']}, '{$_POST['content']}', now(), {$member_id}, 1)";
    execute($link,$query);
    $id = $link->insert_id;
    $query = "UPDATE sfk_reply set quote_id = {$id} where id = {$_GET['reply_id']}";
    execute($link,$query);
    if($link->affected_rows) {
        // 更新帖子最新回复时间
        $query = "UPDATE sfk_content SET new_time = NOW() WHERE id = {$_GET['id']}
";
        execute($link,$query);
        skip("show.php?id={$_GET['id']}", 'ok', '回复成功!');
    }else{
        skip($_SERVER['REQUEST_URI'], 'error', '回复失败,请重试!');
    }
}

$template['title']='帖子回复页';
$template['css']=array('style/public.css','style/publish.css');

?>
<?php include 'inc/header.inc.php'?>
<div id="position" class="auto">
    <a href="<?=$_SERVER["HTTP_REFERER"]?>">返回上一页</a> &gt; 回复帖子
</div>
<div id="publish">
    <div>帖子<b style="font-size: 15px"><?=$data_content['name']?></b>发布: <b style="font-size: 15px"><?=$data_content['title']?></b><br>
        <h3>内容：<?=$data_content['content']?></h3>
    </div>
    <br>
    <div>
        回复信息：<br><h3><?=$reply_content['content']?></h3>
    </div>
    <form method="post">
        <textarea name="content" class="content"></textarea>
        <input class="reply" type="submit" name="submit" value="" />
        <div style="clear:both;"></div>
    </form>
</div>
<?php include 'inc/footer.inc.php'?>
